package com.example.test_event_and_callback.infras;

import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Maps {

    /**
     * 将对象通过一个键生成一个索引map
     * 比如将一个List<User> 生成一个Map<Long, User>类型的 通过用户的id进行索引的map 则
     * List<User> users = ...
     * Map<Long, User> userIdMap = Maps.identity(users, User::getId);
     * @param getIdFunction
     * @return
     * @param <T>
     * @param <K>
     */
    public static <T, K> Map<K, T> identify(Collection<T> collection, Function<T, K> getIdFunction) {
        return collection.stream().collect(Maps.identify(getIdFunction));
    }

    public static <T, K> Collector<T, ?, Map<K,T>> identify(Function<T, K> getIdFunction) {
        return Collectors.toMap(getIdFunction, (t1)->t1);
    }


}